package com.george.reversing;

import java.io.File;
import java.io.FileOutputStream;

import com.george.reversing.util.Permission;
import com.george.reversing.util.PermissionDialog;

import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class MainActivityFront extends AppCompatActivity {

    private CameraFragment mCameraFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //去除状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        Window window = getWindow();
        //这一步最好要做，因为如果这两个flag没有清除的话下面没有生效
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        //设置布局能够延伸到状态栏(StatusBar)和导航栏(NavigationBar)里面
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //设置状态栏(StatusBar)颜色透明
        window.setStatusBarColor(Color.TRANSPARENT);
        //设置导航栏(NavigationBar)颜色透明
        window.setNavigationBarColor(Color.TRANSPARENT);
        View decorView = getWindow().getDecorView();
		int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
		decorView.setSystemUiVisibility(uiOptions);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Permission.checkPermission(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Permission.isPermissionGranted(this)) {
            initCameraFragment();
        }
		try {
		FileOutputStream out = new FileOutputStream(new File("/sys/class/gpio/gpio8/value"));  ///sys/power/wake_unlock
		out.write("1".getBytes());
		out.close();
		 out = new FileOutputStream(new File("/sys/class/gpio/gpio69/value"));  ///sys/power/wake_unlock
		out.write("1".getBytes());
		out.close();
		}catch (Exception e) {
			System.out.println(e);
		}
    }
    
    
    

    @Override
	protected void onPause() {
	super.onPause();
	try {
		FileOutputStream out = new FileOutputStream(new File("/sys/class/gpio/gpio8/value"));  ///sys/power/wake_unlock
		out.write("0".getBytes());
		out.close();
		 out = new FileOutputStream(new File("/sys/class/gpio/gpio69/value"));  ///sys/power/wake_unlock
		out.write("0".getBytes());
		out.close();
		}catch (Exception e) {
			System.out.println(e);
		}
	}

	private void initCameraFragment() {
        if (mCameraFragment == null) {
            mCameraFragment = new CameraFragment();
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragment_container, mCameraFragment)
                    .commit();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,  String[] permissions,
                                            int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == Permission.REQUEST_CODE) {
            for (int grantResult : grantResults) {
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    showPermissionDenyDialog();
                    return;
                }
            }
        }
    }

    private void showPermissionDenyDialog() {
        PermissionDialog dialog = new PermissionDialog();
        dialog.show(getSupportFragmentManager(), "PermissionDeny");
    }

}
